package consultorio

import grails.converters.JSON

class SearchController {

    	
	def autocompleteenfermedadajax={
		def result=new HashSet()
		if(!params.term.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")){//si no es numerico
			StringTokenizer st=new StringTokenizer(params.term," ")
			while(st.hasMoreElements()){
				String currentElement=st.nextToken();
				result.addAll(Enfermedad.findAllByNombreLike("${currentElement}%"))
			}
		}else{
			def elementById=Enfermedad.get(Long.parseLong(params.term))
			if(elementById){
				result.add(elementById)
			}
		}
				
		if(result.size()>0){
			def jsonResponse=[]
			result.each{item ->
				jsonResponse.add([id:item.id, label:(item.nombre), value:(item.nombre)])
			}
			render (jsonResponse.sort() as JSON)
		}else{
			render ([] as JSON)
		}
	}
	
	def autocompletemedicamentoajax={
		def result=new HashSet()
		if(!params.term.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")){//si no es numerico
			StringTokenizer st=new StringTokenizer(params.term," ")
			while(st.hasMoreElements()){
				String currentElement=st.nextToken();
				result.addAll(Medicamento.findAllByNombreLike("${currentElement}%"))
			}
		}else{
			def elementById=Medicamento.get(Long.parseLong(params.term))
			if(elementById){
				result.add(elementById)
			}
		}
				
		if(result.size()>0){
			def jsonResponse=[]
			result.each{item ->
				jsonResponse.add([id:item.id, label:(item.nombre), value:(item.nombre)])
			}
			render (jsonResponse.sort() as JSON)
		}else{
			render ([] as JSON)
		}
	}
	
	def autocompletemedicamentodosisajax={
		def result=new HashSet()//si no es numerico
		StringTokenizer st=new StringTokenizer(params.term," ")
		while(st.hasMoreElements()){
			String currentElement=st.nextToken();
			result.addAll(Medicamento.findAllByDosisLike("${currentElement}%"))
		}
				
		if(result.size()>0){
			def jsonResponse=[]
			result.each{item ->
				jsonResponse.add([id:item.id, label:(item.dosis), value:(item.dosis)])
			}
			render (jsonResponse.sort() as JSON)
		}else{
			render ([] as JSON)
		}
	}
	
	def searchadminajax = {
		def AdminObj=Admin.findById(params.id)
		render (contentType: 'text/plain', view:'profile', model:[Admin:AdminObj])
	}
	
	def autocompleteadminajax={
		def result=new HashSet()
		if(!params.term.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")){//si no es numerico
			StringTokenizer st=new StringTokenizer(params.term," ")
			while(st.hasMoreElements()){
				String currentElement=st.nextToken();
				result.addAll(Admin.findAllByNombreLike("${currentElement}%"))
				result.addAll(Admin.findAllByApellidoLike("${currentElement}%"))
				result.addAll(Admin.findAllByEmail("${currentElement}"))
			}
		}else{
			def elementById=Admin.get(Long.parseLong(params.term))
			if(elementById){
				result.add(elementById)
			}
		}
				
		if(result.size()>0){
			def jsonResponse=[]
			result.each{item ->
				jsonResponse.add([id:item.id, label:(item.nombre + " " + item.apellido), value:(item.dni)])
			}
			render (jsonResponse.sort() as JSON)
		}else{
			render ([] as JSON)
		}
	}
	
	def searchmedicoajax = {
		def MedicoObj=Medico.findById(params.id)
		def turnoObj=Turno.findAllByMedico(MedicoObj)
		render (contentType: 'text/plain', view:'profile', model:[Medico:MedicoObj, turnos:turnoObj])
	}
	
	def autocompletemedicoajax={
		def result=new HashSet()
		if(!params.term.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")){//si no es numerico
			StringTokenizer st=new StringTokenizer(params.term," ")
			while(st.hasMoreElements()){
				String currentElement=st.nextToken();
				result.addAll(Medico.findAllByNombreLike("${currentElement}%"))
				result.addAll(Medico.findAllByApellidoLike("${currentElement}%"))
				result.addAll(Medico.findAllByEmail("${currentElement}"))
			}
		}else{
			def elementById=Medico.get(Long.parseLong(params.term))
			if(elementById){
				result.add(elementById)
			}
		}
				
		if(result.size()>0){
			def jsonResponse=[]
			result.each{item ->
				jsonResponse.add([id:item.id, label:(item.nombre + " " + item.apellido), value:(item.dni)])
			}
			render (jsonResponse.sort() as JSON)
		}else{
			render ([] as JSON)
		}
	}
	
	def searchpacienteajax = {
		def pacienteObj=Paciente.findById(params.id)
		def turnoObj=Turno.findAllByPaciente(pacienteObj)
		render (contentType: 'text/plain', view:'profile', model:[paciente:pacienteObj, turnos:turnoObj])
	}
	
	def autocompletepacienteajax={
		def result=new HashSet()
		if(!params.term.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")){//si no es numerico
			StringTokenizer st=new StringTokenizer(params.term," ")
			while(st.hasMoreElements()){
				String currentElement=st.nextToken();
				result.addAll(Paciente.findAllByNombreLike("${currentElement}%"))
				result.addAll(Paciente.findAllByApellidoLike("${currentElement}%"))
				result.addAll(Paciente.findAllByEmail("${currentElement}"))
			}
		}else{
			def elementById=Paciente.get(Long.parseLong(params.term))
			if(elementById){
				result.add(elementById)
			}
		}
				
		if(result.size()>0){
			def jsonResponse=[]
			result.each{item ->
				jsonResponse.add([id:item.id, label:(item.nombre + " " + item.apellido), value:(item.dni)])
			}
			render (jsonResponse.sort() as JSON)
		}else{
			render ([] as JSON)
		}
	}
}
